package com.lrkj.ureport.service.impl;

import com.lrkj.common.core.domain.TreeSelectUp;
import com.lrkj.common.core.domain.entity.ReportVo;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.StringUtils;
import com.lrkj.common.utils.uuid.IdUtils;
import com.lrkj.ureport.domain.ReportConfig;
import com.lrkj.ureport.mapper.ReportConfigMapper;
import com.lrkj.ureport.service.IReportConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 报表配置Service业务层处理
 *
 * @author yajun_ren
 * @date 2022-04-01
 */
@Service
public class ReportConfigServiceImpl implements IReportConfigService {
    @Autowired
    private ReportConfigMapper reportConfigMapper;

    /**
     * 查询报表配置
     *
     * @param id 报表配置ID
     * @return 报表配置
     */
    @Override
    public ReportConfig selectReportConfigById(String id) {
        return reportConfigMapper.selectReportConfigById(id);
    }

    /**
     * 查询报表配置列表
     *
     * @param reportConfig 报表配置
     * @return 报表配置
     */
    @Override
    public List<ReportConfig> selectReportConfigList(ReportConfig reportConfig) {
        return reportConfigMapper.selectReportConfigList(reportConfig);
    }

    /**
     * 新增报表配置
     *
     * @param reportConfig 报表配置
     * @return 结果
     */
    @Override
    public int insertReportConfig(ReportConfig reportConfig) {
        if (reportConfig.getReportConfigs().size() > 0) {
            reportConfig.getReportConfigs().stream().forEach(x -> {
                x.setId(IdUtils.simpleUUID());
                x.setCreateTime(DateUtils.getNowDate());
            });
            this.doSaveList(reportConfig.getReportConfigs());
        }
        return 1;
    }

    /**
     * 修改报表配置
     *
     * @param reportConfig 报表配置
     * @return 结果
     */
    @Override
    public int updateReportConfig(ReportConfig reportConfig) {
        reportConfig.setUpdateTime(DateUtils.getNowDate());
        return reportConfigMapper.updateReportConfig(reportConfig);
    }

    /**
     * 批量删除报表配置
     *
     * @param ids 需要删除的报表配置ID
     * @return 结果
     */
    @Override
    public int deleteReportConfigByIds(String[] ids) {
        return reportConfigMapper.deleteReportConfigByIds(ids);
    }

    /**
     * 删除报表配置信息
     *
     * @param id 报表配置ID
     * @return 结果
     */
    @Override
    public int deleteReportConfigById(String id) {
        return reportConfigMapper.deleteReportConfigById(id);
    }

    /**
     * 批量新增报表配置信息
     *
     * @param reportConfigs 报表配置对象
     */
    public int batchAddData(List<ReportConfig> reportConfigs) {
        return reportConfigMapper.batchAddData(reportConfigs);
    }


    /**
     * 构建前端所需要下拉树结构
     *
     * @return 下拉树结构列表
     */
    @Override
    public List<TreeSelectUp> buildTreeSelect(ReportVo reportVo) {
        List<ReportVo> trees = buildDeptTree(reportConfigMapper.reportViewList(reportVo));
        return trees.stream().map(TreeSelectUp::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param depts 部门列表
     * @return 树结构列表
     */
    public List<ReportVo> buildDeptTree(List<ReportVo> depts) {
        List<ReportVo> returnList = new ArrayList<ReportVo>();
        List<String> tempList = new ArrayList<String>();
        for (ReportVo dept : depts) {
            tempList.add(dept.getId());
        }
        for (Iterator<ReportVo> iterator = depts.iterator(); iterator.hasNext(); ) {
            ReportVo dept = (ReportVo) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(dept.getParentId())) {
                recursionFn(depts, dept);
                returnList.add(dept);
            }
        }
        if (returnList.isEmpty()) {
            returnList = depts;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<ReportVo> list, ReportVo t) {
        // 得到子节点列表
        List<ReportVo> childList = getChildList(list, t);
        t.setChildren(childList);
        for (ReportVo tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<ReportVo> getChildList(List<ReportVo> list, ReportVo t) {
        List<ReportVo> tlist = new ArrayList<ReportVo>();
        Iterator<ReportVo> it = list.iterator();
        while (it.hasNext()) {
            ReportVo n = (ReportVo) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().equals(t.getId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<ReportVo> list, ReportVo t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }

}
